package com.aidong.exchange.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.exchange.biz.MstExchangeTicketsBiz;
import com.aidong.exchange.entity.MstExchangeTickets;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 兑换券action
 * @author user
 *
 */
@ParentPackage("json")
@Namespace("/")
public class MstExchangeTicketsAction implements ModelDriven<MstExchangeTickets>{
	
	private Map<String, Object> map; // 返回的json数据
	private MstExchangeTickets mstExchangeTickets;//兑换券实体
	private MstExchangeTicketsBiz mstExchangeTicketsBiz;//兑换券service
	
	@Override
	public MstExchangeTickets getModel() {
		// TODO Auto-generated method stub
		if (mstExchangeTickets == null) {
			mstExchangeTickets = new MstExchangeTickets();
		}
		return mstExchangeTickets;
	}
	
	
	/**
	 * 返回的json数据
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	/**
	 * 兑换券service
	 * @param mstExchangeTicketsBiz
	 */
	@Autowired
	public void setMstExchangeTicketsBiz(MstExchangeTicketsBiz mstExchangeTicketsBiz) {
		this.mstExchangeTicketsBiz = mstExchangeTicketsBiz;
	}
	
	@Action(value="exchangeticketusable",results={@Result(name="succ",type="json",params={"root","map"})})
	public String exchangeTicketUsable() {
		map = new HashMap<String, Object>();
		int campaignId = mstExchangeTickets.getCampaignId();
		//int orderId = mstExchangeTickets.getOrderId();
		String exchangeTicketNum = mstExchangeTickets.getExchangeTicketNum();
		int goodsType = mstExchangeTickets.getGoodsType();
		
		if (null != exchangeTicketNum && 0 != campaignId) {
			//判断兑换券是否可用
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("exchangeTicketNum", exchangeTicketNum.toUpperCase());
			paraMap.put("campaignId", campaignId);
			paraMap.put("goodsType", goodsType);
			
			
			Map<String, Object> resultMap = new HashMap<String,Object>();
			resultMap = mstExchangeTicketsBiz.exchangeTicketUsable(paraMap);
			if (resultMap == null) {
				map.put("code", 0);
				map.put("msg", "兑换券无效");
			}else {
				//判断是否过期--可以点击兑换就说明没有过期
				//String validDateBegin = resultMap.get("validDateBegin").toString();
				//String validDateEnd = resultMap.get("validDateEnd").toString();
				map.put("code", 1);
				map.put("msg", "兑换券可用");
			}
		}else {
			map.put("code", 0);
			map.put("msg", "参数缺失");
		}
		return "succ";
	}
}
